import {ref} from 'vue'
import {BasicColumn} from '/@/components/Table';
import {FormSchema} from '/@/components/Table';
import { queryTreeList } from '/@/api/common/api';
import { rules} from '/@/utils/helper/validator';
import { render } from '/@/utils/common/renderUtils';
//列表数据
let initTreeList = ref([])
const treeList = ref([])
async function tableData() {
  initTreeList.value = await queryTreeList()
  treeList.value = flatten(initTreeList.value)
}
function flatten(arr) {
  let res = [];
  for (let i = 0; i < arr.length; i++) {
    const child = arr[i].children;
    if (child?.length > 0) {
      res = res.concat(flatten(child));
    }
    res.push(arr[i]);
  }
  return res;
}
tableData()

export const columns: BasicColumn[] = [

   {
    title: '编号',
    align:"center",
    dataIndex: 'serialNumber',
    width: 60
   },
   {
    title: '姓名',
    align:"center",
    dataIndex: 'name',
    width: 80
   },
   {
    title: '性别',
    align:"center",
    dataIndex: 'sex',
    customRender:({text}) =>{
      return text==1?'男':text==2?'女':''
    },
    width: 60
   },
   {
    title: '出生日期',
    align:"center",
    dataIndex: 'departTime',
    customRender:({text}) =>{
      return !text?"":(text.length>10?text.substr(0,10):text)
    },
    width: 100
   },
   {
    title: '任职部门',
    align:"center",
    dataIndex: 'departmentEmploymenId',
    customRender:({text}) => treeList.value.filter(item => item.id == text)[0]?.departName,
    width: 160
   },
  //  {
  //   title: '任职部门id',
  //   align:"center",
  //   dataIndex: 'departmentEmploymenId'
  //  },
   {
    title: '入职日期',
    align:"center",
    dataIndex: 'dateBoard',
    width: 100,
    customRender:({text}) => !text?"":(text.length>10?text.substr(0,10):text)
   },
   {
    title: '关联账号',
    align:"center",
    dataIndex: 'associatedAccount',
    width: 100
   },
  //  {
  //   title: '关联账号id',
  //   align:"center",
  //   dataIndex: 'associatedAccountId'
  //  },
   {
    title: '员工编号',
    align:"center",
    dataIndex: 'employeeCode',
    width: 100,
   },
   {
    title: '证件号码',
    align:"center",
    dataIndex: 'idNumber',
    width: 140,
   },
   {
    title: '任职岗位',
    align:"center",
    dataIndex: 'position',
    width: 100,
   },
  //  {
  //   title: '任职岗位id',
  //   align:"center",
  //   dataIndex: 'positionId'
  //  },
   {
    title: '联系电话',
    align:"center",
    dataIndex: 'phone',
    width: 100,
   },
   {
    title: '身份证地址',
    align:"center",
    dataIndex: 'idCardAddress',
    width: 140,
   },
   {
    title: '第三方编码',
    align:"center",
    dataIndex: 'thirdPartyCode',
    ifShow: false
   },
   {
    title: '备注',
    align:"center",
    dataIndex: 'remark'
   },
];
//查询数据
export const searchFormSchema: FormSchema[] = [
  {
    label: "编码",
    field: "serialNumber",
    component: 'Input',
    colProps: { span: 6 },
  },
  {
    label: "姓名",
    field: "name",
    component: 'Input',
    colProps: { span: 6 },
  }
];

//表单数据
export const formSchema: FormSchema[] = [

  {
    label: '编号',
    field: 'serialNumber',
    component: 'Input',
  },
  {
    label: '姓名',
    field: 'name',
    component: 'Input',
  },
  {
    label: '性别',
    field: 'sex',
    component: 'Select',
    componentProps:{
      options:[{label:'男',value:1},{label:'女',value:2}],
      stringToNumber: true,
    }
  },
  {
    label: '出生日期',
    field: 'departTime',
    component: 'DatePicker',
  },
  {
    label: '任职部门',
    field: 'departmentEmployment',
    component: 'Input',
    show:false
  },
  {
    label: '任职部门',
    field: 'departmentEmploymenId',
    component: 'JSelectDept',
    componentProps: ({}) => {
      return {
        multiple: false,
        showButton: false,
        checkable: false
      }
    },

  },
  {
    label: '入职日期',
    field: 'dateBoard',
    component: 'DatePicker',
  },
  {
    label: '关联账号',
    field: 'associatedAccount',
    component: 'Input',
  },
  {
    label: '关联账号id',
    field: 'associatedAccountId',
    component: 'Input',
    show:false
  },
  {
    label: '员工编号',
    field: 'employeeCode',
    component: 'Input',
  },
  {
    label: '证件号码',
    field: 'idNumber',
    component: 'Input',
  },
  {
    label: '任职岗位',
    field: 'position',
    component: 'Input',
  },
  {
    label: '任职岗位id',
    field: 'positionId',
    component: 'Input',
    show:false
  },
  {
    label: '联系电话',
    field: 'phone',
    component: 'Input',
    rules: rules.rule('phone', false),
  },
  {
    label: '身份证地址',
    field: 'idCardAddress',
    component: 'Input',
  },
  {
    label: '第三方编码',
    field: 'thirdPartyCode',
    component: 'Input',
    ifShow: false
  },
  {
    label: '备注',
    field: 'remark',
    component: 'InputTextArea',
  },
	// TODO 主键隐藏字段，目前写死为ID
	{
	  label: '',
	  field: 'id',
	  component: 'Input',
	  show: false
	},
];



/**
* 流程表单调用这个方法获取formSchema
* @param param
*/
export function getBpmFormSchema(_formData): FormSchema[]{
  // 默认和原始表单保持一致 如果流程中配置了权限数据，这里需要单独处理formSchema
  return formSchema;
}